package com.yangyang.autobackup;

import cn.hutool.db.Entity;
import cn.hutool.db.Session;
import cn.hutool.db.ds.simple.SimpleDataSource;
import org.apache.commons.io.FileUtils;
import org.junit.Test;

import javax.sql.DataSource;
import java.io.File;
import java.sql.*;
import java.util.Arrays;
import java.util.List;

public class TestSqlite {


    static final String localPath = "C:/Users/yangyang/Desktop/data-sync";

    @Test
    public void t1() throws Exception {
        File dbFile = FileUtils.getFile(localPath, "data_info.db3");
        if (!dbFile.exists()) {
            dbFile.createNewFile();
        }
        String url = "jdbc:sqlite:" + FileUtils.getFile(localPath, "data_info.db3");

        String sql = "SELECT COUNT(*) FROM LIKE_VIDEO";

        try (Connection conn = DriverManager.getConnection(url);
             Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql)) {
            while (rs.next()) {
                String cnt = rs.getString(1);
                System.out.println(cnt);
            }

        }
    }

    @Test
    public void huToolDB3() throws Exception {
        String url = "jdbc:sqlite:" + "D:\\TEMP\\yyds.test-admin\\src\\main\\resources\\douyin.db3";
        String sql = "SELECT * FROM LIKE_VIDEO";

        DataSource ds = new SimpleDataSource(url, "", "");
        Session session = Session.create(ds);
        List<Entity> query = session.query(sql);
        session.close();
        System.out.println(query);
    }


    @Test
    public void huToolDB3Add() throws Exception {
        String url = "jdbc:sqlite:" + "D:\\TEMP\\yyds.test-admin\\src\\main\\resources\\douyin.db3";
        DataSource ds = new SimpleDataSource(url, "", "");
        Session session = Session.create(ds);
        List<Entity> entityList = getMySQLList();
        int[] insert1 = session.insert(entityList);
        session.close();
        int sum = Arrays.stream(insert1).sum();
        System.out.println("sum=" + sum);
    }

    public List<Entity> getMySQLList() throws SQLException {
        String url = "jdbc:mysql:///douyin";
        String user = "root";
        String password = "root";
        String sql = "SELECT * FROM LIKE_VIDEO";

        DataSource ds = new SimpleDataSource(url, user, password);
        Session session = Session.create(ds);
        List<Entity> query = session.query(sql);
        session.close();
        return query;
    }
}
